package com.itx.gatewayx.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itx.gatewayx.domain.NewsComment;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 新闻评论Mapper接口
 *
 * @author itx
 */
@Mapper
public interface NewsCommentMapper extends BaseMapper<NewsComment> {
    
    /**
     * 分页查询新闻的根评论
     * 
     * @param page 分页参数
     * @param newsId 新闻ID
     * @return 分页评论信息
     */
    @Select({
        "SELECT c.*, u.username, u.avatar as user_avatar ",
        "FROM news_comment c ",
        "LEFT JOIN user u ON c.user_id = u.id ",
        "WHERE c.news_id = #{newsId} AND c.parent_id = 0 AND c.del_flag = 0 ",
        "ORDER BY c.create_time DESC"
    })
    IPage<NewsComment> selectRootComments(Page<NewsComment> page, @Param("newsId") String newsId);
    
    /**
     * 查询评论的回复列表
     * 
     * @param parentId 父评论ID
     * @return 回复列表
     */
    @Select({
        "SELECT c.*, u.username, u.avatar as user_avatar, ru.username as reply_to_user ",
        "FROM news_comment c ",
        "LEFT JOIN user u ON c.user_id = u.id ",
        "LEFT JOIN user ru ON c.reply_to_user_id = ru.id ",
        "WHERE c.parent_id = #{parentId} AND c.del_flag = 0 ",
        "ORDER BY c.create_time ASC"
    })
    List<NewsComment> selectReplies(@Param("parentId") Long parentId);
} 